package yxy.game.pm2.chat.module;

import yxy.game.pm2.bean.area.ChatChannelType;
import yxy.game.pm2.Result;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;

public class ChatChannel_Union extends BaseChatChannelModule {

	private String unionCode;

	public ChatChannel_Union(String unionCode) {
		this.unionCode = unionCode;
	}

	public String getUnionCode() {
		return unionCode;
	}

	@Override
	protected int historySize() {
		return 20;
	}

	@Override
	protected ChatChannelType getChannelType() {
		return ChatChannelType.CHAT_UNION;
	}

	@Override
	protected String key() {
		return String.format("UNION:%s:CHAT:MSG", getUnionCode());
	}

	@Override
	protected String keySub() {
		return String.format("UNION:%s:CHAT:SUB", getUnionCode());
	}

	public static ChatChannel_Union get(String playerCode) {
		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			return null;
		}
		UnionBean union = entry.getUnionBean();
		return new ChatChannel_Union(union.getCode());
	}

	public static ChatChannel_Union getByUnionCode(String unionCode) {
		// 检查是否已经有联盟
		UnionBean union = UnionBean.get(unionCode);
		if (union == null) {
			return null;// 没有联盟
		}
		return new ChatChannel_Union(union.getCode());
	}

	public void delete() {
		touch().delete();
		touchSubscriber().delete();
	}

}
